<?php
class filebox  extends inputbox {

	public function __construct( $value = ''){
		parent::__construct($value);
		$this->validAttributes[] = 'value';
		$this->validAttributes[] = 'size';
		$this->tag = 'file';
		$this->validmimetypes = array();
		
		$this->mimetypes = array();
		$this->mimetypes['jpe'] = 'image/jpeg';
		$this->mimetypes['jpeg'] = 'image/jpeg';
		$this->mimetypes['jpg'] = 'image/jpeg';
		$this->mimetypes['png'] = 'image/png';		
		$this->mimetypes['gif'] = 'image/gif';
		$this->mimetypes['ief'] = 'image/ief';
		$this->mimetypes['tif'] = 'image/tiff';
		$this->mimetypes['tif'] = 'image/tiff';
		$this->mimetypes['tiff'] = 'image/tiff';
		$this->mimetypes['stl'] = 'application/SLA';
		$this->mimetypes['step'] = 'application/STEP';
		$this->mimetypes['stp'] = 'application/STEP';
		$this->mimetypes['dwg'] = 'application/acad';
		$this->mimetypes['ez'] = 'application/andrew-inset';
		$this->mimetypes['ccad'] = 'application/clariscad';
		$this->mimetypes['drw'] = 'application/drafting';
		$this->mimetypes['tsp'] = 'application/dsptype';
		$this->mimetypes['dxf'] = 'application/dxf';
		$this->mimetypes['xls'] = 'application/excel';
		$this->mimetypes['unv'] = 'application/i-deas';
		$this->mimetypes['jar'] = 'application/java-archive';
		$this->mimetypes['hqx'] = 'application/mac-binhex40';
		$this->mimetypes['cpt'] = 'application/mac-compactpro';
		$this->mimetypes['pot'] = 'application/vnd.ms-powerpoint';
		$this->mimetypes['pps'] = 'application/vnd.ms-powerpoint';
		$this->mimetypes['ppt'] = 'application/vnd.ms-powerpoint';
		$this->mimetypes['ppz'] = 'application/vnd.ms-powerpoint';
		$this->mimetypes['doc'] = 'application/msword';
		$this->mimetypes['bin'] = 'application/octet-stream';
		$this->mimetypes['class'] = 'application/octet-stream';
		$this->mimetypes['dms'] = 'application/octet-stream';
		$this->mimetypes['exe'] = 'application/octet-stream';
		$this->mimetypes['lha'] = 'application/octet-stream';
		$this->mimetypes['lzh'] = 'application/octet-stream';
		$this->mimetypes['oda'] = 'application/oda';
		$this->mimetypes['ogg'] = 'application/ogg';
		$this->mimetypes['ogm'] = 'application/ogg';
		$this->mimetypes['pdf'] = 'application/pdf';
		$this->mimetypes['pgp'] = 'application/pgp';
		$this->mimetypes['ai'] = 'application/postscript';
		$this->mimetypes['eps'] = 'application/postscript';
		$this->mimetypes['ps'] = 'application/postscript';
		$this->mimetypes['prt'] = 'application/pro_eng';
		$this->mimetypes['rtf'] = 'application/rtf';
		$this->mimetypes['set'] = 'application/set';
		$this->mimetypes['smi'] = 'application/smil';
		$this->mimetypes['smil'] = 'application/smil';
		$this->mimetypes['sol'] = 'application/solids';
		$this->mimetypes['vda'] = 'application/vda';
		$this->mimetypes['mif'] = 'application/vnd.mif';
		$this->mimetypes['xlc'] = 'application/vnd.ms-excel';
		$this->mimetypes['xll'] = 'application/vnd.ms-excel';
		$this->mimetypes['xlm'] = 'application/vnd.ms-excel';
		$this->mimetypes['xls'] = 'application/vnd.ms-excel';
		$this->mimetypes['xlw'] = 'application/vnd.ms-excel';
		$this->mimetypes['cod'] = 'application/vnd.rim.cod';
		$this->mimetypes['arj'] = 'application/x-arj-compressed';
		$this->mimetypes['bcpio'] = 'application/x-bcpio';
		$this->mimetypes['vcd'] = 'application/x-cdlink';
		$this->mimetypes['pgn'] = 'application/x-chess-pgn';
		$this->mimetypes['cpio'] = 'application/x-cpio';
		$this->mimetypes['csh'] = 'application/x-csh';
		$this->mimetypes['deb'] = 'application/x-debian-package';
		$this->mimetypes['dcr'] = 'application/x-director';
		$this->mimetypes['dir'] = 'application/x-director';
		$this->mimetypes['dxr'] = 'application/x-director';
		$this->mimetypes['dvi'] = 'application/x-dvi';
		$this->mimetypes['pre'] = 'application/x-freelance';
		$this->mimetypes['spl'] = 'application/x-futuresplash';
		$this->mimetypes['gtar'] = 'application/x-gtar';
		$this->mimetypes['gz'] = 'application/x-gunzip';
		$this->mimetypes['gz'] = 'application/x-gzip';
		$this->mimetypes['hdf'] = 'application/x-hdf';
		$this->mimetypes['ipx'] = 'application/x-ipix';
		$this->mimetypes['ips'] = 'application/x-ipscript';
		$this->mimetypes['js'] = 'application/x-javascript';
		$this->mimetypes['skd'] = 'application/x-koan';
		$this->mimetypes['skm'] = 'application/x-koan';
		$this->mimetypes['skp'] = 'application/x-koan';
		$this->mimetypes['skt'] = 'application/x-koan';
		$this->mimetypes['latex'] = 'application/x-latex';
		$this->mimetypes['lsp'] = 'application/x-lisp';
		$this->mimetypes['scm'] = 'application/x-lotusscreencam';
		$this->mimetypes['mif'] = 'application/x-mif';
		$this->mimetypes['bat'] = 'application/x-msdos-program';
		$this->mimetypes['com'] = 'application/x-msdos-program';
		$this->mimetypes['exe'] = 'application/x-msdos-program';
		$this->mimetypes['cdf'] = 'application/x-netcdf';
		$this->mimetypes['nc'] = 'application/x-netcdf';
		$this->mimetypes['pl'] = 'application/x-perl';
		$this->mimetypes['pm'] = 'application/x-perl';
		$this->mimetypes['rar'] = 'application/x-rar-compressed';
		$this->mimetypes['sh'] = 'application/x-sh';
		$this->mimetypes['shar'] = 'application/x-shar';
		$this->mimetypes['swf'] = 'application/x-shockwave-flash';
		$this->mimetypes['sit'] = 'application/x-stuffit';
		$this->mimetypes['sv4cpio'] = 'application/x-sv4cpio';
		$this->mimetypes['sv4crc'] = 'application/x-sv4crc';
		$this->mimetypes['tar.gz'] = 'application/x-tar-gz';
		$this->mimetypes['tgz'] = 'application/x-tar-gz';
		$this->mimetypes['tar'] = 'application/x-tar';
		$this->mimetypes['tcl'] = 'application/x-tcl';
		$this->mimetypes['tex'] = 'application/x-tex';
		$this->mimetypes['texi'] = 'application/x-texinfo';
		$this->mimetypes['texinfo'] = 'application/x-texinfo';
		$this->mimetypes['man'] = 'application/x-troff-man';
		$this->mimetypes['me'] = 'application/x-troff-me';
		$this->mimetypes['ms'] = 'application/x-troff-ms';
		$this->mimetypes['roff'] = 'application/x-troff';
		$this->mimetypes['t'] = 'application/x-troff';
		$this->mimetypes['tr'] = 'application/x-troff';
		$this->mimetypes['ustar'] = 'application/x-ustar';
		$this->mimetypes['src'] = 'application/x-wais-source';
		$this->mimetypes['zip'] = 'application/x-zip-compressed';
		$this->mimetypes['zip'] = 'application/zip';
		$this->mimetypes['tsi'] = 'audio/TSP-audio';
		$this->mimetypes['au'] = 'audio/basic';
		$this->mimetypes['snd'] = 'audio/basic';
		$this->mimetypes['kar'] = 'audio/midi';
		$this->mimetypes['mid'] = 'audio/midi';
		$this->mimetypes['midi'] = 'audio/midi';
		$this->mimetypes['mp2'] = 'audio/mpeg';
		$this->mimetypes['mp3'] = 'audio/mpeg';
		$this->mimetypes['mpga'] = 'audio/mpeg';
		$this->mimetypes['au'] = 'audio/ulaw';
		$this->mimetypes['aif'] = 'audio/x-aiff';
		$this->mimetypes['aifc'] = 'audio/x-aiff';
		$this->mimetypes['aiff'] = 'audio/x-aiff';
		$this->mimetypes['m3u'] = 'audio/x-mpegurl';
		$this->mimetypes['wax'] = 'audio/x-ms-wax';
		$this->mimetypes['wma'] = 'audio/x-ms-wma';
		$this->mimetypes['rpm'] = 'audio/x-pn-realaudio-plugin';
		$this->mimetypes['ram'] = 'audio/x-pn-realaudio';
		$this->mimetypes['rm'] = 'audio/x-pn-realaudio';
		$this->mimetypes['ra'] = 'audio/x-realaudio';
		$this->mimetypes['wav'] = 'audio/x-wav';
		$this->mimetypes['pdb'] = 'chemical/x-pdb';
		$this->mimetypes['xyz'] = 'chemical/x-pdb';
		$this->mimetypes['ras'] = 'image/cmu-raster';
		$this->mimetypes['ras'] = 'image/x-cmu-raster';
		$this->mimetypes['pnm'] = 'image/x-portable-anymap';
		$this->mimetypes['pbm'] = 'image/x-portable-bitmap';
		$this->mimetypes['pgm'] = 'image/x-portable-graymap';
		$this->mimetypes['ppm'] = 'image/x-portable-pixmap';
		$this->mimetypes['rgb'] = 'image/x-rgb';
		$this->mimetypes['xbm'] = 'image/x-xbitmap';
		$this->mimetypes['xpm'] = 'image/x-xpixmap';
		$this->mimetypes['xwd'] = 'image/x-xwindowdump';
		$this->mimetypes['iges'] = 'model/iges';
		$this->mimetypes['igs'] = 'model/iges';
		$this->mimetypes['mesh'] = 'model/mesh';
		$this->mimetypes['msh'] = 'model/mesh';
		$this->mimetypes['silo'] = 'model/mesh';
		$this->mimetypes['vrml'] = 'model/vrml';
		$this->mimetypes['wrl'] = 'model/vrml';
		$this->mimetypes['css'] = 'text/css';
		$this->mimetypes['htm'] = 'text/html';
		$this->mimetypes['html'] = 'text/html';
		$this->mimetypes['html'] = 'text/html';
		$this->mimetypes['asc'] = 'text/plain';
		$this->mimetypes['asc'] = 'text/plain';
		$this->mimetypes['c'] = 'text/plain';
		$this->mimetypes['cc'] = 'text/plain';
		$this->mimetypes['f90'] = 'text/plain';
		$this->mimetypes['f'] = 'text/plain';
		$this->mimetypes['h'] = 'text/plain';
		$this->mimetypes['hh'] = 'text/plain';
		$this->mimetypes['m'] = 'text/plain';
		$this->mimetypes['txt'] = 'text/plain';
		$this->mimetypes['rtx'] = 'text/richtext';
		$this->mimetypes['rtf'] = 'text/rtf';
		$this->mimetypes['sgm'] = 'text/sgml';
		$this->mimetypes['sgml'] = 'text/sgml';
		$this->mimetypes['tsv'] = 'text/tab-separated-values';
		$this->mimetypes['jad'] = 'text/vnd.sun.j2me.app-descriptor';
		$this->mimetypes['etx'] = 'text/x-setext';
		$this->mimetypes['xml'] = 'text/xml';
		$this->mimetypes['dl'] = 'video/dl';
		$this->mimetypes['fli'] = 'video/fli';
		$this->mimetypes['flv'] = 'video/flv';
		$this->mimetypes['gl'] = 'video/gl';
		$this->mimetypes['mp2'] = 'video/mpeg';
		$this->mimetypes['mp4'] = 'video/mp4';
		$this->mimetypes['mpe'] = 'video/mpeg';
		$this->mimetypes['mpeg'] = 'video/mpeg';
		$this->mimetypes['mpg'] = 'video/mpeg';
		$this->mimetypes['mov'] = 'video/quicktime';
		$this->mimetypes['qt'] = 'video/quicktime';
		$this->mimetypes['viv'] = 'video/vnd.vivo';
		$this->mimetypes['vivo'] = 'video/vnd.vivo';
		$this->mimetypes['fli'] = 'video/x-fli';
		$this->mimetypes['asf'] = 'video/x-ms-asf';
		$this->mimetypes['asx'] = 'video/x-ms-asx';
		$this->mimetypes['wmv'] = 'video/x-ms-wmv';
		$this->mimetypes['wmx'] = 'video/x-ms-wmx';
		$this->mimetypes['wvx'] = 'video/x-ms-wvx';
		$this->mimetypes['avi'] = 'video/x-msvideo';
		$this->mimetypes['movie'] = 'video/x-sgi-movie';
		$this->mimetypes['mime'] = 'www/mime';
		$this->mimetypes['ice'] = 'x-conference/x-cooltalk';
		$this->mimetypes['vrm'] = 'x-world/x-vrml';
		$this->mimetypes['vrml'] = 'x-world/x-vrml';		
		$this->mimetypes['vrml'] = 'image/vnd.adobe.photoshop';		
				
	}
	
	
	public function addValidMimeType($type){
		$this->validmimetypes[] = $type;	
	}
	
	public function setValidMimeTypes($validMimeTypes = null){
		if ( is_array($validMimeTypes)){
			foreach ($validMimeTypes as $type){
				$this->validmimetypes[] = $type;
			}
		} elseif ( is_scalar($validMimeTypes)) {
			$this->validmimetypes[] = $type;
		}
	}
	
	public function validate(){	
		if ( $this->hasBeenValidated ){
			return $this->isValid();
		} else {
			$this->hasBeenValidated = 1;
		}

		if ( empty($_POST)) return 1;
		if ( empty($_FILES)) return 1;
		
		$name = $this->attributes['name'];
		if ( isset($_FILES[$name]) ){
			//check for error in the transfer
			if ( $_FILES[$name]['error'] > 0 ){

				//UPLOAD_ERR_OK         Value: 0; There is no error, the file uploaded with success.
				//UPLOAD_ERR_INI_SIZE   Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
				//UPLOAD_ERR_FORM_SIZE  Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
				//UPLOAD_ERR_PARTIAL    Value: 3; The uploaded file was only partially uploaded.
				//UPLOAD_ERR_NO_FILE    Value: 4; No file was uploaded.
				//UPLOAD_ERR_NO_TMP_DIR Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
				//UPLOAD_ERR_CANT_WRITE Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
				//UPLOAD_ERR_EXTENSION  Value: 8; File upload stopped by extension. Introduced in PHP 5.2.0.
				
				
				if ( $_FILES[$name]["name"] == ''){
					$this->errorMessage = 'No se especifico ningun archivo a enviar';
					$this->fail();	
					return $this->isValid();	
				} elseif ( $_FILES[$name]['error'] == 1 ){
					$this->errorMessage = 'El archivo especificado excede el tamaño máximo de archivo permitido.';
					$this->fail();	
					return $this->isValid();
				} elseif ( $_FILES[$name]['error'] == 2 ){
					$this->errorMessage = 'El archivo especificado excede el tamaño máximo de archivo permitido.';
					$this->fail();	
					return $this->isValid();		
				} elseif ( $_FILES[$name]['error'] == 3 ){
					$this->errorMessage = 'El archivo especificado no fue transferido por completo.';
					$this->fail();	
					return $this->isValid();									
				} elseif ( $_FILES[$name]['error'] == 4 ){
					$this->errorMessage = 'No se transfirió ningún archivo.';
					$this->fail();	
					return $this->isValid();									
				} elseif ( $_FILES[$name]['error'] == 6 ){
					$this->errorMessage = 'No se encontró ningun directorio temporal donde guardar la foto.';
					$this->fail();	
					return $this->isValid();									
				} elseif ( $_FILES[$name]['error'] == 7){
					$this->errorMessage = 'Ocurrió un error al intentar guardar archivo en disco.';
					$this->fail();	
					return $this->isValid();									
				} elseif ( $_FILES[$name]['error'] == 8 ){
					$this->errorMessage = 'Transferencia abortada por encontrar archivo de extensión inválida.';
					$this->fail();	
					return $this->isValid();									
				} else {							
					$this->errorMessage = 'Ocurrió un error al enviar el archivo';
					$this->fail();
					return $this->isValid();
				}
			}

			//check for known mime types
			$isvaliddatatype = 0;
			if ( isset($_FILES[$name]["type"])){
								
				foreach ($this->mimetypes as $ext => $mimetype){
					if ( $_FILES[$name]["type"] == $mimetype){
						$isvaliddatatype = 1;
						break;
					}
				}
				if ( ! $isvaliddatatype ){
					$this->errorMessage = 'El archivo enviado no es de formato reconocido.';
					$this->fail();
					return $this->isValid();
				}
			}
			
			//check for the specified valid mime types			
			if ( count($this->validmimetypes)){

				$acceptedmimetypes = array();
				foreach ( $this->validmimetypes as $key => $extension){
					if ( isset($this->mimetypes[$extension])){
						$acceptedmimetypes[] = 	$this->mimetypes[$extension];
					}
				}
				
				$isvaliddatatype = 0;			
				foreach ($acceptedmimetypes as $key => $mimetype){
					if ( $_FILES[$name]["type"] == $mimetype){
						$isvaliddatatype = 1;
						break;
					}
				}				
				if ( ! $isvaliddatatype ){
					$this->errorMessage = 'El formato del archivo enviado no es aceptado en este formulario.';
					$this->fail();	
					return $this->isValid();
				}				
			}
			return 1;
			
		} else {
			return 1;
		}
	}
}
?>